home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / C / LIB / UNIXLIB37B / !UnixLib37 / src / stdio / c / fwrite < prev    next >
Text File  |  1996-11-09  |  1KB  |  66 lines

  1. /****************************************************************************
  2.  *
  3.  * $Source: /unixb/home/unixlib/source/unixlib37/src/stdio/c/RCS/fwrite,v $
  4.  * $Date: 1996/05/06 09:01:34 $
  5.  * $Revision: 1.2 $
  6.  * $State: Rel $
  7.  * $Author: unixlib $
  8.  *
  9.  * $Log: fwrite,v $
  10.  * Revision 1.2  1996/05/06 09:01:34  unixlib
  11.  * Updates to sources made by Nick Burrett, Peter Burwood and Simon Callan.
  12.  * Saved for 3.7a release.
  13.  *
  14.  * Revision 1.1  1996/04/19 21:32:42  simon
  15.  * Initial revision
  16.  *
  17.  ***************************************************************************/
  18.  
  19. static const char rcs_id[] = "$Id: fwrite,v 1.2 1996/05/06 09:01:34 unixlib Rel $";
  20.  
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <stdio.h>
  24. #include <unistd.h>
  25.  
  26. __STDIOLIB__
  27.  
  28. int
  29. __fwrite (register FILE * f, register char *s, register int _n)
  30. {
  31.   register int n, i, b, g = f->flag;
  32.  
  33.   if ((g & (_IOWRITE | _IOERR | _IOEOF)) != _IOWRITE)
  34.     return (-1);
  35.  
  36.   b = (g & _IONBF) ? 1 : f->bufsiz;
  37.  
  38.   n = _n;
  39.  
  40.   while (n)
  41.     {
  42.       if (i = ((n > f->o_cnt) ? f->o_cnt : n))    /* write buffer */
  43.     {
  44.       memcpy (f->o_ptr, s, i);
  45.       f->o_cnt -= i, f->o_ptr += i;
  46.       n -= i, s += i;
  47.     }
  48.       if (n || ((g & _IOLBF) && i && s[-1] == '\n'))
  49.     {
  50.       if (__flsbuf (-1, f) < 0)    /* flush buffer */
  51.         return (_n - n);
  52.     }
  53.       while (n >= b)        /* direct write() */
  54.     {
  55.       if ((i = write (f->fd, s, n - (n % b))) < b)
  56.         {
  57.           f->flag |= _IOERR;
  58.           return (_n - n + i);
  59.         }
  60.       f->pos += i, n -= i, s += i;
  61.     }
  62.     }
  63.  
  64.   return (_n);
  65. }
  66.